from django.contrib import admin
from illness_protection.models import *
import requests
from django.http import HttpResponseRedirect

# Register your models here.


@admin.register(ImportantArea)
class ImportantAreaAdmin(admin.ModelAdmin):
    list_display = ['value', 'value2', 'label', 'label2', 'region', "delete"]
    search_fields = ['label', 'region']
    list_filter = ('delete', )
    readonly_fields = ['value']


@admin.register(ImportantCity)
class ImportantCityAdmin(admin.ModelAdmin):
    list_display = ['value', 'label', 'region', 'level', "delete"]
    search_fields = ['label', 'region']
    list_filter = ('delete', )
    readonly_fields = ['value']


@admin.register(OperatePeople)
class OperatePeopleAdmin(admin.ModelAdmin):
    list_display = ('name','xgh','lx','xiaoban_type','deal_method','is_deal','err_reason','create_by','create_time')
    readonly_fields = ['name','xgh','lx','xiaoban_type','deal_method','datacenter_name','create_by','is_deal',"err_reason",'create_time','update_time']
    search_fields = ['xgh', 'name', 'lx']

    ordering = ('-create_time', )

    def has_delete_permission(self, request, obj=None):
    	# 禁用删除按钮
        return False

    def save_model(self, request, obj, form, change):
        if not obj.is_deal:
            obj.is_deal = 0
        obj.create_by = request.user.username
        super().save_model(request, obj, form, change)
        
        # print(str(r.status_code))

    def do_update(self, request, queryset):
        url = "http://yqfk.dgut.edu.cn/auth/people/centerAdd"
        r = requests.get(url=url)
        return HttpResponseRedirect('/admin/illness_protection/operatepeople')
        
    def get_readonly_fields(self, request, obj=None):
        return ['datacenter_name','create_by','is_deal',"err_reason",'create_time','update_time']

    do_update.short_description = '执行增删操作'
    actions = [do_update]


class ReadonlyFieldsMixin(object):
    def get_readonly_fields(self, request, obj=None):
        if obj:
            return super(ReadonlyFieldsMixin, self).get_readonly_fields(request, obj)
        else:
            return tuple()


class XiaobanTypeInlinx(admin.StackedInline):
    model = XiaobanType
    extra = 0

    def has_add_permission(self, request, obj=None): 
        return False

    def has_delete_permission(self, request, obj=None):
    	# 禁用删除按钮
        return False


@admin.register(PeopleAtSchoolNew)
class PeopleAtSchoolAdmin(admin.ModelAdmin):
    list_display = ('xh','xm','sj','bjdm','yxdm','type')
    readonly_fields = ['xh','xm','sj','bjdm','yxdm','lx','sfzjlxm','sfzjh']
    search_fields = ['xh','xm']
    save_on_top = True


    inlines = [XiaobanTypeInlinx]
    # fieldsets = (
    #     ("基本信息", { "fields": ['xh','xm','sj','bjdm','yxdm','lx']}),
    #     ("基本信息", { "fields": ['sfzjh']}),
    # )
    

    # def get_readonly_fields(self,request,obj=None):
    #     if not request.user.is_superuser and not request.user.can_edit:
    #         return [f.name for f in self.models._meta.fields]
    #     return self.readonly_fields

    def has_add_permission(self, request):
		# 禁用添加按钮
        return False

    def has_delete_permission(self, request, obj=None):
    	# 禁用删除按钮
        return False

    def get_actions(self, request):
    	# 在actions中去掉‘删除’操作
        actions = super(PeopleAtSchoolAdmin, self).get_actions(request)
        if request.user.username[0].upper() != 'J':
            if 'delete_selected' in actions:
                del actions['delete_selected']
        return actions


@admin.register(DailySubmitInfoNew)
class DailySubmitInfoNewAdmin(admin.ModelAdmin):
    list_display = ['submit_time', 'username', 'name', 'faculty', 'health_situation', 'body_temperature', 'current_country', 'current_province', 'current_city', 'current_district']
    search_fields = ['username', 'name']
    list_filter = ['submit_time', 'health_situation', 'faculty']

    ordering = ['-submit_time', ]


